import {createRouter, createWebHistory, type RouteRecordRaw} from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import {useUserStore} from "../store/user.ts";
import {Session} from "../utils/storage.ts";
import { useRoutesList} from "../store/routesList.ts";
import {initBackEndControlRoutes} from "./initRouter.ts";
import { storeToRefs } from 'pinia';
const routes = [


    // {
    //     path: '/a',
    //     name: 'la',
    //     meta:  {
    //         requireAuth:true
    //     },
    //     component: () => import('../layout/index.vue'),
    //     children: [
    //         {
    //             path: 'a',
    //             name: 'a',
    //             component: () => import('../views/base/vip.vue')
    //         },
    //         {
    //             path: 'b',
    //             name: 'b',
    //             component: () => import('../views/base/barber.vue')
    //         }
    //         ]
    // },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/login/Login.vue') // 假设登录页组件位于该路径
    },
    // {
    //     path: '/base',
    //     // name: 'base',
    //     component: () => import('../layout/Layout.vue'),
    //     meta: {
    //         requireAuth: true
    //     },
    //     children: [
    //         {
    //             path: '',
    //             name: 'home',
    //             component: () => import('../components/Home.vue')
    //         },
    //         {
    //             path: 'contact',
    //             name: 'contact',
    //             component: () => import('../components/Contact.vue')
    //         },
    //         {
    //             path: 'about',
    //             name: 'about',
    //             component: () => import('../components/About.vue')
    //         }
    //     ]
    // },

]
export const notFoundAndNoPower = [
    {
        path: '/401',
        name: 'Unauthorized',
        component: () => import('../views/error/401.vue')
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: () => import('../views/error/404.vue')
    }
]
/**
 * 定义静态路由（默认路由）
 * 前端添加路由，请在此处加
 */
export const dynamicRoutes: Array<RouteRecordRaw> = [
    {
                path: 'a',
                name: 'a',
                component: () => import('../views/base/vip.vue')
    },
];
/**
 *  基础性路由
 *
 * 所有节点都是挂载此节点下
 */
export const baseRoutes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: '/',
        component: () => import('../layout/index.vue'),
        meta: {
            isKeepAlive: true,
        },
        children: [],
    },
];
const router = createRouter({
    // history: createWebHistory(process.env.BASE_URL),
    history: createWebHistory(),
    routes
})
// 是否正在加载动态路由
let isDynamicRouteAdded = false


const whiteList = ['/login']

router.beforeEach(async (to,from) => {
    let userStore = useUserStore()
    const token = Session.getToken();
    // if (!to.meta.requireAuth) {
    //     // 路由不需要认证
    //     return true
    // }
    if (whiteList.includes(to.path)){
        return true;
    }
    //token不存在
    if (!token  && to.path !== '/login'){
        Session.clear();
        // 将用户重定向到登录页面
        return { name: 'Login' }
    }else if (token && to.path === '/login') {
        return { name: 'a' }
        // NProgress.done();
    }else {
        let storesRoutesList = useRoutesList();
        const { routesList } = storeToRefs(storesRoutesList);
        if (routesList.value.length === 0) {
            // 后端控制路由：路由数据初始化，防止刷新时丢失
            await initBackEndControlRoutes();
            return ({ path: to.path, query: to.query });
        } else {
            return true
        }
    }
})
export default router
